from datetime import datetime
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

from core.models import BaseModel
from account.models import Account

import requests


class Diy(BaseModel):

    # 用户账号
    account         = models.ForeignKey(Account, on_delete=models.SET_NULL, null = True, blank = True)

    uuid            = models.CharField('UUID', unique=True, max_length = 128)

    title           = models.TextField('名称')

    desc            = models.TextField('描述', null = True, blank = True)

    price           = models.FloatField('价格', default=0)

    share           = models.BooleanField('分享', default=True)

    weapp_qrcode    = models.ImageField('小程序码', upload_to='diy', null = True, blank = True)

    def __str__(self):
        return '{}-{}'.format(self.pk, self.title)

    class Meta:
        verbose_name = "配置清单"
        verbose_name_plural = "配置清单"


class Ware(BaseModel):

    diy             = models.ForeignKey(Diy, on_delete=models.CASCADE, related_name='wares')

    title           = models.TextField('名称', null = True, blank = True)

    desc            = models.TextField('描述', null = True, blank = True)

    price           = models.FloatField('价格', default=0)

    count           = models.IntegerField('数量', default=0)

    image           = models.ImageField('图片', upload_to='ware', null = True, blank = True)

    brand           = models.CharField('品牌', max_length = 128, null = True, blank = True)

    def __str__(self):
        return '{}-{}'.format(self.pk, self.title)

    class Meta:
        verbose_name = "配置元件"
        verbose_name_plural = "配置元件"


# 点赞列表
class Like(BaseModel):
    account         = models.ForeignKey(Account, on_delete=models.SET_NULL, null = True, blank = True)
    diy             = models.ForeignKey(Diy, on_delete=models.CASCADE, null = True, blank = True)

    class Meta:
        unique_together = ('account', 'diy')
        verbose_name = "点赞列表"
        verbose_name_plural = "点赞列表"


# 收藏列表
class Collect(BaseModel):
    account         = models.ForeignKey(Account, on_delete=models.SET_NULL, null = True, blank = True)
    diy             = models.ForeignKey(Diy, on_delete=models.CASCADE, null = True, blank = True)

    class Meta:
        unique_together = ('account', 'diy')
        verbose_name = "收藏列表"
        verbose_name_plural = "收藏列表"


# 浏览列表
class Browse(BaseModel):
    account         = models.ForeignKey(Account, on_delete=models.SET_NULL, null = True, blank = True)
    diy             = models.ForeignKey(Diy, on_delete=models.CASCADE, null = True, blank = True)

    class Meta:
        verbose_name = "浏览列表"
        verbose_name_plural = "浏览列表"
